package wifiunion.com.notice.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.smart.colorview.utils.ResUtils;

import java.math.BigDecimal;
import java.util.ArrayList;

import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.RecyclerItemClick;
import wifiunion.com.notice.entity.PushEntity;
import wifiunion.com.notice.utils.glideutils.NewGlideCircleHasBackgroudTransform;

/**
 * Created by Administrator on 2017/4/24.
 */

public class RollerAdapter extends RecyclerView.Adapter<RollerViewHolder> {

    private Context mContext;

    /**
     * item项的宽
     */
    private int mPerWidth;

    private int mScreenHeight;

    private ArrayList<PushEntity> mData = new ArrayList<>();

    private RecyclerItemClick mItemClick;

    private int circleSize;

    /**
     * 0 系统默认
     * 1 自定义
     */
    private int mType;

    private int textColor;


    private int mAlpha;

    private String imageUri = "";


    public RollerAdapter(Context context, ArrayList<PushEntity> data, RecyclerItemClick itemclick) {
        mContext = context;
        mData = data;
        mPerWidth = getPerSize();
        mScreenHeight = getScreenHeight();
        mItemClick = itemclick;
        circleSize = mContext.getResources().getDimensionPixelSize(R.dimen.dp_300);

        if (!TextUtils.isEmpty(Constants.SERVER_PORT)) {

            imageUri = "http://" + Constants.SERVER_IP + ":" + Constants.SERVER_PORT + "/";
        } else {

            imageUri = "http://" + Constants.SERVER_IP + "/";
        }

    }


    @Override
    public RollerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        RelativeLayout view;
//        View view;
//        if (mType == 0) {
//            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.roller_item, parent, false);
//        } else {
//            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.roller_sef_item, parent, false);
//        }

        view = (RelativeLayout) LayoutInflater.from(parent.getContext()).inflate(R.layout.roller_all_item, parent, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mPerWidth, mScreenHeight);
        view.setLayoutParams(params);
        RollerViewHolder rv = new RollerViewHolder(view, mItemClick);
        return rv;
    }

    @Override
    public void onBindViewHolder(RollerViewHolder holder, int pos) {
        PushEntity pe = mData.get(pos);

        String picurl = pe.getPicUrl();
        int stranger = pe.getIsStranger();
        int alarmstatus = pe.getAlarmState();
        String name = pe.getName();
        String content = pe.getContent();
        String temp = pe.getTemperature();
        holder.mBgIv.setVisibility(View.GONE);
        holder.mHeaderIv.setVisibility(View.GONE);
        holder.mStatusIv.setVisibility(View.GONE);
        holder.mNameTv.setVisibility(View.GONE);
        holder.mNoticeTv.setVisibility(View.GONE);
        holder.mTempTv.setVisibility(View.GONE);

        holder.mSefBgIv.setVisibility(View.GONE);
        holder.mSefHeaderIv.setVisibility(View.GONE);
        holder.mSefStatusIv.setVisibility(View.GONE);
        holder.mSefNameTv.setVisibility(View.GONE);
        holder.mSefNoticeTv.setVisibility(View.GONE);
        holder.mSefTempTv.setVisibility(View.GONE);
        int timeShow = pe.getIsShowTime();

        if (mType == 0) {
            holder.defvv.setVisibility(View.VISIBLE);
            holder.sefvv.setVisibility(View.GONE);
            holder.mNameTv.setTextColor(Color.WHITE);
            holder.mNoticeTv.setTextColor(Color.WHITE);
            holder.mTempTv.setTextColor(Color.WHITE);
        } else if (mType == 1) {
            holder.defvv.setVisibility(View.GONE);
            holder.sefvv.setVisibility(View.VISIBLE);
            holder.mSefNameTv.setTextColor(textColor);
            holder.mSefNoticeTv.setTextColor(textColor);
            holder.mSefTempTv.setTextColor(textColor);
        }

        if (stranger == 0) {
            if (alarmstatus == 0) {

                if (mType == 0) {
                    holder.mBgIv.setVisibility(View.VISIBLE);
                    holder.mHeaderIv.setVisibility(View.VISIBLE);
                    holder.mStatusIv.setVisibility(View.VISIBLE);
                    holder.mNameTv.setVisibility(View.VISIBLE);
                    holder.mNoticeTv.setVisibility(View.VISIBLE);
                    holder.mBgIv.setImageResource(R.drawable.item_blue_bg);
                    //不告警
                    Glide.with(mContext).load(imageUri + picurl)
                            .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                            .centerCrop().transform(new NewGlideCircleHasBackgroudTransform(mContext, 1, circleSize)
                            /*new GlideCircleHasBackgroudTransform(mContext,1)*/).into(holder.mHeaderIv);
                    holder.mStatusIv.setImageResource(R.drawable.status_normal);
                    holder.mNameTv.setText(name);
                    holder.mNoticeTv.setText(content);
                    if (!TextUtils.isEmpty(temp)) {
                        holder.mTempTv.setVisibility(View.VISIBLE);
                        holder.mTempTv.setText(String.format(mContext.getResources().getString(R.string.temperature), temp));
                    } else {
                        holder.mTempTv.setVisibility(View.GONE);
                    }
                } else if (mType == 1) {
                    holder.mSefBgIv.setVisibility(View.VISIBLE);
                    holder.mUpIv.setVisibility(View.VISIBLE);
                    holder.mDownIv.setVisibility(View.VISIBLE);
                    holder.mSefHeaderIv.setVisibility(View.VISIBLE);
                    holder.mSefStatusIv.setVisibility(View.VISIBLE);
                    holder.mSefNameTv.setVisibility(View.VISIBLE);
                    holder.mSefNoticeTv.setVisibility(View.VISIBLE);

                    holder.mUpIv.setImageResource(R.drawable.blue_up);
                    holder.mDownIv.setImageResource(R.drawable.blue_down);
                    holder.mSefBgIv.setImageAlpha(mAlpha);

                    //不告警
                    Glide.with(mContext).load(imageUri + picurl)
                            .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                            .centerCrop().transform(new NewGlideCircleHasBackgroudTransform(mContext, 1, circleSize)
                            /*new GlideCircleHasBackgroudTransform(mContext,1)*/).into(holder.mSefHeaderIv);
                    holder.mSefStatusIv.setImageResource(R.drawable.status_normal);
                    holder.mSefNameTv.setText(name);
                    holder.mSefNoticeTv.setText(content);
                    if (!TextUtils.isEmpty(temp)) {
                        holder.mSefTempTv.setVisibility(View.VISIBLE);
                        holder.mSefTempTv.setText(String.format(mContext.getResources().getString(R.string.temperature), temp));
                    } else {
                        holder.mSefTempTv.setVisibility(View.GONE);
                    }
                }


            } else if (alarmstatus == 1) {


                if (mType == 0) {
                    holder.mBgIv.setVisibility(View.VISIBLE);
                    holder.mBgIv.setImageResource(R.drawable.item_red_bg);
                    holder.mHeaderIv.setVisibility(View.VISIBLE);
                    holder.mStatusIv.setVisibility(View.VISIBLE);
                    holder.mNameTv.setVisibility(View.VISIBLE);
                    holder.mNoticeTv.setVisibility(View.VISIBLE);

                    //告警
                    Glide.with(mContext).load(imageUri + picurl)
                            .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                            .centerCrop().transform(new NewGlideCircleHasBackgroudTransform(mContext, 2, circleSize)
                            /*new GlideCircleHasBackgroudTransform(mContext,2)*/).into(holder.mHeaderIv);
                    holder.mStatusIv.setImageResource(R.drawable.status_danger);
                    holder.mNameTv.setText(name);
                    holder.mNoticeTv.setText(content);

                    if (!TextUtils.isEmpty(temp)) {
                        holder.mTempTv.setVisibility(View.VISIBLE);
                        holder.mTempTv.setText(String.format(mContext.getResources().getString(R.string.temperature), temp));
                    } else {
                        holder.mTempTv.setVisibility(View.GONE);
                    }
                } else if (mType == 1) {
                    holder.mSefBgIv.setVisibility(View.VISIBLE);
                    holder.mUpIv.setVisibility(View.VISIBLE);
                    holder.mDownIv.setVisibility(View.VISIBLE);
                    holder.mUpIv.setImageResource(R.drawable.red_up);
                    holder.mDownIv.setImageResource(R.drawable.red_down);
                    holder.mSefBgIv.setImageAlpha(mAlpha);

                    holder.mSefHeaderIv.setVisibility(View.VISIBLE);
                    holder.mSefStatusIv.setVisibility(View.VISIBLE);
                    holder.mSefNameTv.setVisibility(View.VISIBLE);
                    holder.mSefNoticeTv.setVisibility(View.VISIBLE);


                    //告警
                    Glide.with(mContext).load(imageUri + picurl)
                            .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                            .centerCrop().transform(new NewGlideCircleHasBackgroudTransform(mContext, 2, circleSize)
                            /*new GlideCircleHasBackgroudTransform(mContext,2)*/).into(holder.mSefHeaderIv);
                    holder.mSefStatusIv.setImageResource(R.drawable.status_danger);
                    holder.mSefNameTv.setText(name);
                    holder.mSefNoticeTv.setText(content);
                    if (!TextUtils.isEmpty(temp)) {
                        holder.mSefTempTv.setVisibility(View.VISIBLE);
                        holder.mSefTempTv.setText(String.format(mContext.getResources().getString(R.string.temperature), temp));
                    } else {
                        holder.mSefTempTv.setVisibility(View.GONE);
                    }
                }


            } else {
                holder.mBgIv.setVisibility(View.GONE);
                holder.mHeaderIv.setVisibility(View.GONE);
                holder.mStatusIv.setVisibility(View.GONE);
                holder.mNameTv.setVisibility(View.GONE);
                holder.mNoticeTv.setVisibility(View.GONE);
                holder.mTempTv.setVisibility(View.GONE);
                holder.mSefBgIv.setVisibility(View.GONE);
                holder.mSefHeaderIv.setVisibility(View.GONE);
                holder.mSefStatusIv.setVisibility(View.GONE);
                holder.mSefNameTv.setVisibility(View.GONE);
                holder.mSefNoticeTv.setVisibility(View.GONE);
                holder.mSefTempTv.setVisibility(View.GONE);
                holder.mUpIv.setVisibility(View.GONE);
                holder.mDownIv.setVisibility(View.GONE);

            }

        } else if (stranger == 1) {


            if (mType == 0) {
                holder.mBgIv.setVisibility(View.VISIBLE);
                holder.mBgIv.setImageResource(R.drawable.item_yellow_bg);
                holder.mHeaderIv.setVisibility(View.VISIBLE);
                holder.mStatusIv.setVisibility(View.VISIBLE);
                holder.mNameTv.setVisibility(View.INVISIBLE);
                holder.mNoticeTv.setVisibility(View.VISIBLE);

                //陌生人
                Glide.with(mContext).load(imageUri + picurl)
                        .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                        .centerCrop().transform(new NewGlideCircleHasBackgroudTransform(mContext, 3, circleSize)
                        /*new GlideCircleHasBackgroudTransform(mContext,3)*/).into(holder.mHeaderIv);
                holder.mStatusIv.setImageResource(R.drawable.status_unkonw);
                holder.mNameTv.setText(TextUtils.isEmpty(name) ? "未登记" : name);
                holder.mNoticeTv.setText(content);
                if (!TextUtils.isEmpty(temp)) {
                    holder.mTempTv.setVisibility(View.VISIBLE);
                    holder.mTempTv.setText(String.format(mContext.getResources().getString(R.string.temperature), temp));
                } else {
                    holder.mTempTv.setVisibility(View.GONE);
                }
            } else if (mType == 1) {
                holder.mSefBgIv.setVisibility(View.VISIBLE);
                holder.mUpIv.setVisibility(View.VISIBLE);
                holder.mDownIv.setVisibility(View.VISIBLE);
                holder.mUpIv.setImageResource(R.drawable.yellow_up);
                holder.mDownIv.setImageResource(R.drawable.yellow_down);
                holder.mSefBgIv.setImageAlpha(mAlpha);

                holder.mSefHeaderIv.setVisibility(View.VISIBLE);
                holder.mSefStatusIv.setVisibility(View.VISIBLE);
                holder.mSefNameTv.setVisibility(View.INVISIBLE);
                holder.mSefNoticeTv.setVisibility(View.VISIBLE);
                //陌生人
                Glide.with(mContext).load(imageUri + picurl)
                        .placeholder(R.drawable.mainview_default_header).error(R.drawable.mainview_default_header)
                        .centerCrop().transform(new NewGlideCircleHasBackgroudTransform(mContext, 3, circleSize)
                        /*new GlideCircleHasBackgroudTransform(mContext,3)*/).into(holder.mSefHeaderIv);
                holder.mSefStatusIv.setImageResource(R.drawable.status_unkonw);
                holder.mSefNameTv.setText(TextUtils.isEmpty(name) ? "未登记" : name);
                holder.mSefNoticeTv.setText(content);
                if (!TextUtils.isEmpty(temp)) {
                    holder.mSefTempTv.setVisibility(View.VISIBLE);
                    holder.mSefTempTv.setText(String.format(mContext.getResources().getString(R.string.temperature), temp));
                } else {
                    holder.mSefTempTv.setVisibility(View.GONE);
                }
            }


        } else {
            holder.mBgIv.setVisibility(View.GONE);
            holder.mHeaderIv.setVisibility(View.GONE);
            holder.mStatusIv.setVisibility(View.GONE);
            holder.mNameTv.setVisibility(View.GONE);
            holder.mNoticeTv.setVisibility(View.GONE);
            holder.mTempTv.setVisibility(View.GONE);

            holder.mSefBgIv.setVisibility(View.GONE);
            holder.mUpIv.setVisibility(View.GONE);
            holder.mDownIv.setVisibility(View.GONE);
            holder.mSefHeaderIv.setVisibility(View.GONE);
            holder.mSefStatusIv.setVisibility(View.GONE);
            holder.mSefNameTv.setVisibility(View.GONE);
            holder.mSefNoticeTv.setVisibility(View.GONE);
            holder.mSefTempTv.setVisibility(View.GONE);
        }

        if (pe.getIsShowTime() == 0) {
            holder.timeRl.setVisibility(View.GONE);
            holder.seftimeRl.setVisibility(View.GONE);
        } else {
            if (mType == 0) {
                holder.timeRl.setVisibility(View.VISIBLE);
                holder.firstTv.setText("显示初次见面   " + pe.getFirstTime());
                holder.lastTv.setText("上次见面时间   " + pe.getLastTime());
            } else {
                holder.seftimeRl.setVisibility(View.VISIBLE);
                holder.seffirstTv.setTextColor(textColor);
                holder.seflastTv.setTextColor(textColor);
                holder.seffirstTv.setText("显示初次见面   " + pe.getFirstTime());
                holder.seflastTv.setText("上次见面时间   " + pe.getLastTime());
            }
//            holder.currentTv.setText("此时此刻   " + pe.getNowTime());
        }


    }

    @Override
    public int getItemCount() {
        return mData.size();
    }


    public void setType(int type) {
        mType = type;
    }

    /**
     * 设置文字颜色
     */
    public void setTextColor(int selectPos) {
        selectPos = selectPos;
        textColor = mContext.getResources().getColor(ResUtils.getColorId(mContext, "smart_color_" + selectPos));

    }

    public void setBackGroundColor(int bgColor) {
        if (bgColor == 100) {
            mAlpha = 255;
        } else {
            int a = bgColor;
            int b = 100;

            double result = new BigDecimal((float) a / b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            BigDecimal a1 = new BigDecimal(Double.toString((result)));
            BigDecimal b1 = new BigDecimal(Double.toString(255));
            mAlpha = a1.multiply(b1).intValue();

        }

    }


    /**
     * 屏宽
     *
     * @return
     */
    private int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Display dis = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        dis.getMetrics(metrics);
        return metrics.widthPixels;
    }

    /**
     * 屏高
     *
     * @return
     */
    private int getScreenHeight() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Display dis = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        dis.getMetrics(metrics);
        return metrics.heightPixels;
    }

    /**
     * 每项的宽
     *
     * @return
     */
    private int getPerSize() {
        return getScreenWidth() / 3;
    }
}
